#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

struct playground {
	int *key;
	int len;
} pg;

void d_error()
{
	printf("恭喜，你没有毁灭进步\n");
	exit(1);
}

int d_input(int argc, char **argv)
{
	int i, tmp;
	pg.key = (int *)malloc((argc - 1) * sizeof(int));
	pg.len = argc -1;
	printf("Data before sorting：\n");
	for (i = 1; i < argc; i++) {
		pg.key[i] = atoi(argv[i]);
		printf("%d ", pg.key[i]);
	}
	printf("\n");
	return 0;
}

void sort()
{
	int i = 0, j = 0, tmp, index;
	for (i = 2; i < pg.len; i++) {
		tmp = pg.key[i];
		for (j = i - 1; j >= 1; j--) {
			if (tmp < pg.key[j]) {
				pg.key[j + 1] = pg.key[j];
				pg.key[j] = tmp;
			} else {
				break;
			}
		}
	}
}

void print_output()
{
	int i = 1;
	printf("Data after sorting:\n");
	for (; i <= pg.len; i++) {
		printf("%d ", pg.key[i]);
	}
	printf("\n");
}

int main(int argc, char **argv)
{
	if (argc <= 1) {
		printf("亲，请输入你要处理的数据!\n");
		d_error();
	}
	if (argv == NULL) {
		printf("Param error\n");
		d_error();
	}
	if (0 > d_input(argc, argv)) {
		printf("Deal with input error!");
		d_error();
	}
	sort();
	print_output();
	return 0;
}
